package dnl.confij.apps;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import com.thoughtworks.xstream.XStream;

public class XStreamPersister implements Persister {
	private XStream xstream = new XStream();

	@Override
	public Object load(File f) {
		try {
			Object fromXML = xstream.fromXML(new FileReader(f));
			return fromXML;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public void save(File f, Object entityToSave) {
		try {
			xstream.toXML(entityToSave, new FileWriter(f));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void addXStreamAlias(String alias, Class<?> clax) {
		xstream.alias(alias, clax);
	}

	public XStream getXStream() {
		// xstream.setMode(XStream.XPATH_RELATIVE_REFERENCES);
		// xstream.alias("perfpack-config", PerfPackConfig.class);
		// xstream.alias("classic-run-config",
		// ClassicStandaloneAppConfiguration.class);
		// xstream.alias("jar-run-config", ExecutableJarAppConfiguration.class);
		return xstream;
	}
}
